package com.example.prom.ok.y2023.work.au10tix;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.alibaba.fastjson.JSON;
import com.example.prom.ok.y2023.work.maxmind.MaxMindIpReaderVO;
import com.example.prom.strategy.excel.CustomCellStyleStrategy;
import com.example.prom.strategy.excel.CustomColumnWidthStrategy;
import com.example.prom.utils.HttpClientSingleton;
import com.example.prom.utils.HttpNewUtils;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.alibaba.excel.EasyExcelFactory.write;

@RestController
public class Au10tixLivenessHandler {

    static {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        List<ch.qos.logback.classic.Logger> loggerList = loggerContext.getLoggerList();
        loggerList.forEach(logger -> {
            logger.setLevel(Level.INFO);
        });
    }

    public static void main(String[] args) throws Exception {
        File fileDir = new File("C:\\Users\\hp\\Desktop\\ok\\work\\0726_0727_liveness_API8");
        File[] fileList = fileDir.listFiles();
        List<Au10tixExcelPo> resultList = new ArrayList<>();
        System.out.println("数量："+fileList.length);
        int i = 0;
        for (File fileItem : fileList) {
            i++;
            System.out.println("当前是第 " + i + " 个");
            System.out.println(fileItem.getAbsolutePath());
            Map<String,String> queries = new HashMap<>();
            queries.put("mode","combined");
            Map<String, String> headers = new HashMap<>();
            headers.put("api-url","https://eus-api.au10tixservices.com");
            headers.put("Authorization","Bearer eyJraWQiOiJjeVcyZkxwYVYySjVUdVAydzhtak5DakgzVnotSWhhN0lSV1Vod0lnZGtzIiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULmRsTWJoSXVNLVpEblBuQk84SkppeGxJZDRuT3l2OWdNb3JtLWEzRDYwRGsiLCJpc3MiOiJodHRwczovL2xvZ2luLmF1MTB0aXguY29tL29hdXRoMi9hdXMzbWx0czVzYmU5V0Q4VjM1NyIsImF1ZCI6ImF1MTB0aXgiLCJpYXQiOjE2OTA1Njg5NTEsImV4cCI6MTY5MDU2OTU1MSwiY2lkIjoiMG9haHAwamwxOHBOUlhWd1gzNTciLCJzY3AiOlsib2NzL3Njb3BlOm1vYmlsZXNkayIsInNkYyIsImZlYyIsInBmbCIsIm1vYmlsZXNkayJdLCJzdWIiOiIwb2FocDBqbDE4cE5SWFZ3WDM1NyIsImFwaVVybCI6Imh0dHBzOi8vZXVzLWFwaS5hdTEwdGl4c2VydmljZXMuY29tIiwiYm9zVXJsIjoiaHR0cHM6Ly9ib3MtZXVzLXdlYi5hdTEwdGl4c2VydmljZXMuY29tIiwiY2xpZW50T3JnYW5pemF0aW9uTmFtZSI6Ik9LY29pbiIsImNsaWVudE9yZ2FuaXphdGlvbklkIjoiNjE3In0.OR5IGPMxytlQXBFLL3ZSRvN2O4oGS0gXa0A4usUfnqvMYAqxwhvKfhTw2pS-o-3IjFBttTtfQQ8MgBKUwzVRe1ZFs8RouC7zhMRXTmtn-gunwKRQR8FuMAqUEsMdZkn_tBSe03HV93OAEBtgchzgKydeOL07kqb7k2mA---FcW7buWWXfUcle8ZoPTHc_ToIQsn67cZUqF2x8WN-BXS3MAQdpNT8Jqz5mkCjPSKlN6zW3lHeIngdtc2Zn6oGzHEv3q5inAI6oprFapeg7V8mORN9iG9hFi4IwAloyNMGppl6JbYBurZSmedmrnfwHNh5J_suZ-A7XzeKvtIXWKze4g");
            File file = new File(fileItem.getAbsolutePath());
            Map<String, File> files = new HashMap<>();
            files.put("file", file);
            HttpResponse httpResponse = HttpNewUtils.doPostByFormData("https://eus-api.au10tixservices.com/pfl/v2/check_liveness", null,
                    HttpClientSingleton.getLongCustomHttpClient(), headers, queries, null, files);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            String result = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8);
            System.out.println(statusCode);
            System.out.println(result);
            Au10tixExcelPo au10tixExcelPo = JSON.parseObject(result, Au10tixExcelPo.class);
            au10tixExcelPo.setFileName(file.getName());
            resultList.add(au10tixExcelPo);
        }
        writeExcel(resultList);

    }

    public static void writeExcel(List<Au10tixExcelPo> writerList){
        String writeFileName = "C:\\Users\\hp\\Desktop\\ok\\excel\\0726_0727_liveness_API8.xlsx";
        write(writeFileName, Au10tixExcelPo.class).sheet().head(Au10tixExcelPo.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(writerList);
    }

    @GetMapping("au10tix/test")
    public void test(HttpServletRequest request, MultipartFile file){
        try {
            Map<String,String> queries = new HashMap<>();
            queries.put("mode","idrnd");
            Map<String, String> headers = new HashMap<>();
            headers.put("api-url","https://eus-api.au10tixservices.com");
            headers.put("Authorization",request.getHeader("Authorization"));
            HttpResponse httpResponse = HttpNewUtils.doPostByFormFile("https://eus-api.au10tixservices.com/pfl/v2/check_liveness", null,
                    HttpClientSingleton.getLongCustomHttpClient(), headers, queries, null, file);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            String result = EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8);
            System.out.println(statusCode);
            System.out.println(result);
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
